package com.coisini.service.impl;

import com.coisini.constant.MessageConstant;
import com.coisini.constant.StatusConstant;
import com.coisini.context.BaseContext;
import com.coisini.dto.CategoryDTO;
import com.coisini.dto.CategoryPageQueryDTO;
import com.coisini.entity.Category;
import com.coisini.exception.DeletionNotAllowedException;
import com.coisini.mapper.CategoryMapper;
import com.coisini.mapper.DishMapper;
import com.coisini.mapper.SetmealMapper;
import com.coisini.result.PageResult;
import com.coisini.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //设置查询条件
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        //调用mapper层接口进行查询
        List<Category> categories = categoryMapper.pageQuery(categoryPageQueryDTO);
        Page<Category> page= (Page<Category>) categories;

        //封装返回的数据，并将其返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        //创建Category对象
        Category category=new Category();
        //拷贝属性
        BeanUtils.copyProperties(categoryDTO,category);

        //更新最后操作时间
        //更新最后操作人
        /*已在切面类中实现
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
         */

        categoryMapper.update(category);
    }

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     */
    @Override
    public void changeStatus(Integer status, Long id) {
        //构建Category对象
        Category category = Category.builder().id(id).status(status)
//                .updateTime(LocalDateTime.now())
                .build();

        //底层就是修改数据库中的status属性
        categoryMapper.update(category);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void insert(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);

      /*  已在切面类中实现
      category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        */

        categoryMapper.insert(category);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> getByType(Integer type) {
        return categoryMapper.getByType(type);
    }

    /**
     * 根据id删除数据
     * 该操作还需要判断每个类下面是否有数据
     * 如果有数据则不能删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //删除分类数据
        categoryMapper.delete(id);
    }
}
